package com.ycj.aop;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

public class CustomAdvisor implements PointcutAdvisor {

  @Override
  public Advice getAdvice() {
    return new MethodInterceptor() {
        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
          System.out.println("我被调用了...") ;
          return invocation.proceed() ;
        }
    } ;
  }

  @Override
  public boolean isPerInstance() {
    return true ;
  }

  @Override
  public Pointcut getPointcut() {
    return new Pointcut() {
        @Override
        public MethodMatcher getMethodMatcher() {
          return new MethodMatcher() {
              @Override
              public boolean matches(Method method, Class<?> targetClass, Object... args) {
                return false;
              }
              @Override
              public boolean matches(Method method, Class<?> targetClass) {
                return method.isAnnotationPresent(DS.class) ;
              }
              @Override
              public boolean isRuntime() {
                return false ;
              }
          };
       }
            
        @Override
        public ClassFilter getClassFilter() {
          return ClassFilter.TRUE;
        }
     } ;
  }

}